package com.rock.code.province.hn.coll;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rock.code.common.util.SpringContextUtil;
import com.rock.code.common.util.WriteFile;
import com.rock.code.common.util.WriteKPI;
import com.rock.code.datacollconfig.DPtech.dao.TbCollPoolConfDao;
import com.rock.code.datacollconfig.DPtech.dao.TbCollPoolKpiDetailDao;
import com.rock.code.datacollconfig.DPtech.domain.TbCollPoolConf;
import com.rock.code.datacollconfig.DPtech.domain.TbCollPoolKpiDetail;
import com.rock.code.datacollconfig.coll.interfaces.CollDataItemInterface;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 爬虫类，爬取数据用
 *
 * @author zhangli
 * @create 2019/3/12 16:09
 **/
public class H3TelCollImpl implements CollDataItemInterface {


    @Resource
    TbCollPoolKpiDetailDao tbCollPoolKpiDetailDao;

    @Resource
    TbCollPoolConfDao tbCollPoolConfDao;

    @Override
    public void execute(String[] args) throws Exception {

        tbCollPoolKpiDetailDao = (TbCollPoolKpiDetailDao) SpringContextUtil.getBeanByClass(TbCollPoolKpiDetailDao.class);
        tbCollPoolConfDao = (TbCollPoolConfDao) SpringContextUtil.getBeanByClass(TbCollPoolConfDao.class);
        Map map = new HashMap();
        map.put("enable", "1");
        map.put("kbp", "10-19-13");
        List<TbCollPoolConf> confList = tbCollPoolConfDao.getAllPoolConf(map);
        for(TbCollPoolConf _confList : confList){
            doDataCaseH3(_confList);
        }

    }

    private void doDataCaseH3(TbCollPoolConf confList) {

        // 负载信息
        String ip = confList.getIp();
        String userName = confList.getUsername();
        String passWord = confList.getPassword();
        String path = confList.getSwap_path();

        System.out.println(ip);

        // 登陆该负载监控页面并保存cookies
        DetectCaseForH3C detectCase = new DetectCaseForH3C();
        detectCase.initHttpClient();
        detectCase.getMapPARAMS().put("COOKIE", "all_refresh=freshtime_rtlog; SID=PRVPPR7jncCf7KSOsC4a0PAeCt8jAfG4; BACKUP_SID=PRVPPR7jncCf7KSOsC4a0PAeCt8jAfG4");

        String first_url="https://"+ip+"/wnm/frame/login.php?user_name="+userName+"&password="+passWord+"&ssl=true&host="+ip+"&lang=cn";
        String text = detectCase.jet(first_url, "GET","",false);

        String cookie = "vindex==1c=0b=0AB00=0R; lang=cn; supportLang=cn%2Cen; 20000305aba3cd44606ab3fad1f12d85a895=true";
        String seesionId= text.substring(text.indexOf("sessionid"),text.indexOf("&loginid="));
        cookie = cookie.replace("20000305aba3cd44606ab3fad1f12d85a895",seesionId.substring("sessionid=".length(),seesionId.length()));
        detectCase.getMapPARAMS().put("COOKIE", cookie);

        // 虚服务器
        String VScontent = "xml=%3Crpc+message-id%3D'101'+xmlns%3D'urn%3Aietf%3Aparams%3Axml%3Ans%3Anetconf%3Abase%3A1.0'++xmlns%3Aweb%3D'urn%3Aietf%3Aparams%3Axml%3Ans%3Anetconf%3Abase%3A1.0'%3E%3Cget%3E%3Cfilter+type%3D'subtree'%3E%3Ctop+xmlns%3D'http%3A%2F%2Fwww.h3c.com%2Fnetconf%2Fdata%3A1.0'+xmlns%3Aweb%3D'http%3A%2F%2Fwww.h3c.com%2Fnetconf%2Fbase%3A1.0'+xmlns%3Adata%3D'http%3A%2F%2Fwww.h3c.com%2Fnetconf%2Fdata%3A1.0'%3E%3CLB%3E%3CVirtualServerStats%3E%3CVirtualServerStat%3E%3CName%2F%3E%3CChassisID%2F%3E%3CSlotID%2F%3E%3CCPUID%2F%3E%3CTotalConnections%2F%3E%3CActiveConnections%2F%3E%3CMaxConnections%2F%3E%3CConnectionsPerSec%2F%3E%3CMaxConnectionPerSec%2F%3E%3CClientSidePKTsIn%2F%3E%3CInBandwidth%2F%3E%3COutBandwidth%2F%3E%3CClientSidePKTsOut%2F%3E%3CDroppedPackets%2F%3E%3CClientSideBytesIn%2F%3E%3CClientSideBytesOut%2F%3E%3CBandwidth%2F%3E%3CMaxBandwidth%2F%3E%3CInMaxBandwidth%2F%3E%3COutMaxBandwidth%2F%3E%3CReceivedRequests%2F%3E%3CDroppedRequests%2F%3E%3CSentResponses%2F%3E%3CDroppedResponses%2F%3E%3C%2FVirtualServerStat%3E%3C%2FVirtualServerStats%3E%3CGenericVirtualServers%3E%3CGenericVirtualServer%3E%3CName%2F%3E%3CDescription%2F%3E%3CType%3E3%3C%2FType%3E%3CVRF%2F%3E%3CIpv4Address%2F%3E%3CIpv4PrefixLength%2F%3E%3CIpv6Address%2F%3E%3CIpv6PrefixLength%2F%3E%3CPortNumber%2F%3E%3CDefaultServerFarm%2F%3E%3CBackupServerFarm%2F%3E%3CRateLimitInBandwidth%2F%3E%3CRateLimitOutBandwidth%2F%3E%3CStickyGroup%2F%3E%3CPolicy%2F%3E%3CIPParameter%2F%3E%3CMaxConnectionLimit%2F%3E%3CRateLimitConnection%2F%3E%3CRateLimitBandwidth%2F%3E%3CUDPPerPacket%2F%3E%3CServiceEnable%2F%3E%3CState%2F%3E%3CServerFarmInUse%2F%3E%3CBusyProtectEnable%2F%3E%3CStickySynEnable%2F%3E%3CConnSynEnable%2F%3E%3CRSBandwidthProtectOrigin%2F%3E%3C%2FGenericVirtualServer%3E%3C%2FGenericVirtualServers%3E%3C%2FLB%3E%3C%2Ftop%3E%3C%2Ffilter%3E%3C%2Fget%3E%3C%2Frpc%3E&req_menu=M_LB_Stat%2FM_LB_Stat%2FM_LB_ServerStat";
        String VSjsonKey = "VirtualServerStats";
        String VS = "-13:";
        resJsonToText(ip, detectCase, seesionId, VScontent, VSjsonKey, VS, path);

        // 实服务组
        String RSGcontent = "xml=%3Crpc+message-id%3D'101'+xmlns%3D'urn%3Aietf%3Aparams%3Axml%3Ans%3Anetconf%3Abase%3A1.0'++xmlns%3Aweb%3D'urn%3Aietf%3Aparams%3Axml%3Ans%3Anetconf%3Abase%3A1.0'%3E%3Cget%3E%3Cfilter+type%3D'subtree'%3E%3Ctop+xmlns%3D'http%3A%2F%2Fwww.h3c.com%2Fnetconf%2Fdata%3A1.0'+xmlns%3Aweb%3D'http%3A%2F%2Fwww.h3c.com%2Fnetconf%2Fbase%3A1.0'+xmlns%3Adata%3D'http%3A%2F%2Fwww.h3c.com%2Fnetconf%2Fdata%3A1.0'%3E%3CLB%3E%3CServerFarmStats%3E%3CServerFarmStat%3E%3CName%2F%3E%3CChassisID%2F%3E%3CSlotID%2F%3E%3CCPUID%2F%3E%3CTotalConnections%2F%3E%3CActiveConnections%2F%3E%3CConnectionsPerSec%2F%3E%3CServerSidePKTsIn%2F%3E%3CServerSidePKTsOut%2F%3E%3CDroppedPackets%2F%3E%3CServerSideBytesIn%2F%3E%3CServerSideBytesOut%2F%3E%3CBandwidth%2F%3E%3CReceivedRequests%2F%3E%3CDroppedRequests%2F%3E%3CSentResponses%2F%3E%3CDroppedResponses%2F%3E%3C%2FServerFarmStat%3E%3C%2FServerFarmStats%3E%3C%2FLB%3E%3C%2Ftop%3E%3C%2Ffilter%3E%3C%2Fget%3E%3C%2Frpc%3E&req_menu=M_LB_Stat%2FM_LB_Stat%2FM_LB_ServerStat";
        String RSjsonKey = "ServerFarmStats";
        String RS = "-14:";
        resJsonToText(ip, detectCase, seesionId, RSGcontent, RSjsonKey, RS, path);

        // 实服务器
        String RScontent = "xml=%3Crpc+message-id%3D'101'+xmlns%3D'urn%3Aietf%3Aparams%3Axml%3Ans%3Anetconf%3Abase%3A1.0'++xmlns%3Aweb%3D'urn%3Aietf%3Aparams%3Axml%3Ans%3Anetconf%3Abase%3A1.0'%3E%3Cget%3E%3Cfilter+type%3D'subtree'%3E%3Ctop+xmlns%3D'http%3A%2F%2Fwww.h3c.com%2Fnetconf%2Fdata%3A1.0'+xmlns%3Aweb%3D'http%3A%2F%2Fwww.h3c.com%2Fnetconf%2Fbase%3A1.0'+xmlns%3Adata%3D'http%3A%2F%2Fwww.h3c.com%2Fnetconf%2Fdata%3A1.0'%3E%3CLB%3E%3CRealServerStats%3E%3CRealServerStat%3E%3CName%2F%3E%3CChassisID%2F%3E%3CSlotID%2F%3E%3CCPUID%2F%3E%3CTotalConnections%2F%3E%3CActiveConnections%2F%3E%3CMaxConnections%2F%3E%3CConnectionsPerSec%2F%3E%3CMaxConnectionPerSec%2F%3E%3CServerSidePKTsIn%2F%3E%3CInBandwidth%2F%3E%3COutBandwidth%2F%3E%3CServerSidePKTsOut%2F%3E%3CDroppedPackets%2F%3E%3CServerSideBytesIn%2F%3E%3CServerSideBytesOut%2F%3E%3CBandwidth%2F%3E%3CMaxBandwidth%2F%3E%3CMaxInBandwidth%2F%3E%3CMaxOutBandwidth%2F%3E%3CReceivedRequests%2F%3E%3CDroppedRequests%2F%3E%3CSentResponses%2F%3E%3CDroppedResponses%2F%3E%3C%2FRealServerStat%3E%3C%2FRealServerStats%3E%3C%2FLB%3E%3C%2Ftop%3E%3C%2Ffilter%3E%3C%2Fget%3E%3C%2Frpc%3E&req_menu=M_LB_Stat%2FM_LB_Stat%2FM_LB_ServerStat";
        String RSGjsonKey = "RealServerStats";
        String RSG = "-15:";
        resJsonToText(ip, detectCase, seesionId, RScontent, RSGjsonKey, RSG, path);
    }

    private void resJsonToText(String ip, DetectCaseForH3C detectCase, String seesionId, String content, String jsonKey, String type, String path){
        // 服务地址
        String service_url = "https://"+ip+"/wnm/get.j?"+seesionId;

        String jsonStr = detectCase.jet(service_url, "POST", content,false);
        JSONObject jsonObject = JSON.parseObject(jsonStr).getJSONObject("LB");
        JSONArray jsonArray = jsonObject.getJSONArray(jsonKey);

        // 指标信息
        Map map = new HashMap();
        map.put("enable", "1");
        map.put("kbp_id", "10-19-13");
        List<TbCollPoolKpiDetail> list = tbCollPoolKpiDetailDao.getAllPoolKpiDetail(map);

        // 解析信息写入文档工具
        WriteKPI writeKPI = new WriteKPI();
        String dataTime = writeKPI.getNowTime();

        // 解析返回的json信息
        if(jsonArray.size()>0){
            for(TbCollPoolKpiDetail _list : list){
                String key = _list.getKpi_rule();
                String kpi_id = _list.getKpi_id();
                String unit_id = _list.getKbp_id()+type+ip.replace(".", "_");
                for(int i=0; i<jsonArray.size(); i++){
                    JSONObject object = jsonArray.getJSONObject(i);
                    String name = object.getString("Name");
                    int solt = object.getInteger("SlotID");
                    if(solt==1){
                        String value = object.getString(key);
                        if(value != null && !"null".equals(value)){
                            writeKPI.writeKPItoList(value, kpi_id, unit_id+"-"+name, dataTime, "1");
                        }
                    }
                }

            }

            WriteFile wf = new WriteFile();
            wf.writeSevenStrucFile(path, writeKPI.getVECTOR());

        }
    }


}
